package view;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;


import model.*;
import model.database.CalendarWrapper;
import model.schedule.Ad;
import model.schedule.Schedule;
import model.schedule.Scheduler;

public class SchedulerListener implements Observer {
	public SchedulerListener(Scheduler scheduler) {
		scheduler.addObserver(this);
	}

	public void update(Observable arg0, Object arg1) {
		Scheduler scheduler = (Scheduler)arg0;
		if (arg1 instanceof Schedule) {
			Schedule schedule = (Schedule) arg1;
			System.out.println("New schedule created:");

			Iterator<Ad> itr = schedule.iterator();
			CalendarWrapper timeNow = new CalendarWrapper();
			while (itr.hasNext()) {
				Ad ad = itr.next();
				System.out.println("Sleep: "+(ad.getEnd().timeMillis()-timeNow.timeMillis())+"ms");
				System.out.println("Handle ad: "+ad.getId());
				timeNow = ad.getEnd();
			}

			System.out.println();
		} else if (arg1 instanceof String) {
			String message = (String)arg1;
			System.out.println("Scheduler "+(new CalendarWrapper())+": "+message);
			System.out.println();
		}
	}
}
